#include "mainwindow.h"

#include <QList>
#include <QScreen>
#include <string>
#include <QDebug>
#include <QGuiApplication>
#include <QByteArray>
#include <QPainter>

void MainWindow::InitRTImagesWidget()
{
    m_rt_images_widget = new RTImagesWidget();
    m_rt_images_widget->hide();
    m_stack_widget->addWidget(m_rt_images_widget);
}

void MainWindow::InitTagAddrSetWidget()
{
    m_tag_addr_set = new TagAddrSetWidget();
    m_tag_addr_set->hide();
    m_stack_widget->addWidget(m_tag_addr_set);
}

void MainWindow::InitIssServerSettingWidget()
{
    m_loggin_widget = new LogginWidget();
    m_loggin_widget->hide();
    m_stack_widget->addWidget(m_loggin_widget);
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    m_central_widget = new QWidget();
    QScreen* screen = QGuiApplication::primaryScreen();
    QSize size = screen->size();
    m_central_widget->setFixedSize(size.width(), size.height());

    this->setCentralWidget(m_central_widget);

    m_layout = new QVBoxLayout(m_central_widget);
    m_layout->setAlignment(Qt::AlignCenter);
    m_layout->setContentsMargins(0, 10, 10, 50);

    m_central_widget->setStyleSheet("background-color: rgb(200, 200, 200);");

    m_stack_widget = new QStackedWidget();
    m_layout->addWidget(m_stack_widget, 0);

    m_animation = new QPropertyAnimation(m_stack_widget, QByteArray());
    connect(m_animation, SIGNAL(valueChanged(QVariant)),
            this, SLOT(value_change_event(QVariant)));
    connect(m_animation, SIGNAL(finished()),
            this, SLOT(animation_finished()));

    m_switch = new QPushButton();
    m_switch->setText("switch");
    m_layout->addWidget(m_switch, 1);

    m_central_widget->setLayout(m_layout);

    InitRTImagesWidget();
    InitIssServerSettingWidget();
    InitTagAddrSetWidget();

    m_stack_widget->setCurrentWidget(m_rt_images_widget);

    connect(m_switch, SIGNAL(pressed()), this, SLOT(switch_button(void)));

    connect(m_stack_widget, SIGNAL(currentChanged(int)),
            this, SLOT(switch_page(int)));
    qDebug() << "total pages count: " << m_stack_widget->count();
}

void MainWindow::switch_page(int idx) {}

void MainWindow::switch_button()
{
    if (m_animating) {
        return;
    }
    m_animating = true;
    m_curIdx = m_stack_widget->currentIndex();
    m_nextIdx = (m_curIdx + 1) % (m_stack_widget->count());
    // hide current widget
    m_stack_widget->widget(m_curIdx)->hide();
    QRect geo = m_stack_widget->geometry();
    m_animation->setStartValue(geo.width());
    m_animation->setEndValue(0);
    m_animation->setDuration(1000);
    m_animation->start();
}

void MainWindow::value_change_event(QVariant value)
{
    m_value = value;
    this->update();
}

void MainWindow::animation_finished()
{
    m_animating = false;
    m_stack_widget->widget(m_stack_widget->currentIndex())->show();
    m_stack_widget->setCurrentIndex(m_nextIdx);
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    if (!m_animating) {
        return;
    }

    QPainter paint(this);

    // draw previous
    QWidget* curWidget = m_stack_widget->widget(m_curIdx);
    QPixmap pixmap(curWidget->size());
    curWidget->render(&pixmap);
    QRect r = curWidget->geometry();
    double r_value = m_value.toDouble();
    QRectF r1(0.0, 0.0, r_value, r.height());
    QRectF r2(r.width() - r_value, 0, r_value, r.height());
    paint.drawPixmap(r1, pixmap, r2);

    // draw next
    QWidget* nextWidget = m_stack_widget->widget(m_nextIdx);
    r = m_stack_widget->geometry();
    nextWidget->resize(r.width(), r.height());

    QPixmap next_pixmap(curWidget->size());
    curWidget->render(&next_pixmap);
    r = curWidget->geometry();

    QRectF next_r1(r_value, 0.0, r.width() - r_value, r.height());
    QRectF next_r2(0.0, 0.0, r.width() - r_value, r.height());
    paint.drawPixmap(next_r1, next_pixmap, next_r2);
}

MainWindow::~MainWindow() {}
